<template>
  <div style="width: 80%; margin-right: 10%; margin-left: 10%; margin-top: 2%">
    <el-form label-width="120px">
      <el-form-item label="活动标题">
        <el-input v-model="activity.activityName" placeholder="请填写标题" />
      </el-form-item>

      <el-form-item label="活动内容">
        <div style="border: 1px solid #ccc;">
            <Toolbar
                style="border-bottom: 1px solid #ccc"
                :editor="editor"
                :defaultConfig="toolbarConfig"
                :mode="mode"
            />
            <Editor
                style="height: 500px; overflow-y: hidden;"
                v-model="activity.content"
                :defaultConfig="editorConfig"
                :mode="mode"
                ref="editor"
                @onCreated="onCreated"
                @onChanged="onChanged"
            />
        </div>
        </el-form-item>
      <!-- <el-form-item v-else label="活动内容">
        <el-card>
            <div v-html="activity.content"></div>
        </el-card>
      </el-form-item> -->

      <el-form-item label="优先级">
        <el-input-number
          :min="1"
          :max="100"
          :step="1"
          v-model="activity.level"
          controls-position="right"
          placeholder="请填写展示优先级"
        />
      </el-form-item>

      <!-- 活动封面 -->
      <el-form-item label="活动封面">
        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :action="BASE_URL"
          class="avatar-uploader"
        >
          <img :src="imageUrl" style="width: 320px; height: 320px" />
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button v-if="isSave" type="primary" @click="saveOrUpdateActivity"
          >保存</el-button
        >
        <el-button v-else type="primary" @click="saveOrUpdateActivity"
          >添加</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
  // <!-- :action="BASE_API+'/eduoss/fileoss/uploadFile'" -->
  <script>
import activity from "@/api/activity";
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import axios from 'axios'

export default {
  components: { Toolbar,Editor },
  data() {
    return {
      url: "eduoss/fileoss/uploadFile",
      BASE_URL: process.env.VUE_APP_BASE_API + "/weddingoss/fileoss/uploadFile",
      isSave: true,
      activity: {},
      editor: null,
      value: '',
      html: '',
      toolbarConfig: { 
        exexcludeKeys: ["uploadVideo"]
       },
      editorConfig: {
          placeholder: '请输入内容...' ,
          MENU_CONF: {
            uploadImage: {
              customUpload: this.uploadImg,
            },
          },
      },
      mode: 'default', // or 'simple',
      imageUrl:
        "https://img1.baidu.com/it/u=3686273500,1950045651&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1672419600&t=40dcde0e36cec07218f8f480555e7dcc",
    };
  },

  watch: {
    $route(to, from) {
      this.init();
    },
  },
  mounted () {
    
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      if (this.$route.query && this.$route.query.id) {
        // this.id = this.$route.query.id
        this.activity.activityId = this.$route.query.id;
        this.isSave = true;
        activity.getOneById(this.activity.activityId).then((res) => {
          if (res.success) {
            this.activity = res.data.activity;
            this.imageUrl = this.activity.activityUrl;
          } else {
            this.$message({
              message: "系统异常,稍后再试",
              type: "failed",
            });
          }
        });
      } else {
        this.activity.activityUrl = this.imageUrl;
        this.isSave = false;
      }
    },

    handleAvatarSuccess(res, file) {
      this.activity.activityUrl = res.data.url
      this.imageUrl = res.data.url
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    saveOrUpdateActivity() {
      activity.saveOrUpdate(this.activity).then((res) => {
        if (res.success) {
          if (this.isSave) {
            this.$message({
              message: "保存成功",
              type: "success",
            });
          } else {
            this.activity = {};
            this.imageUrl =
              "https://img1.baidu.com/it/u=3686273500,1950045651&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1672419600&t=40dcde0e36cec07218f8f480555e7dcc";
            this.activity.activityUrl = this.imageUrl;
            this.$message({
              message: "添加成功",
              type: "success",
            });
          }
        } else {
          if (this.isSave) {
            this.$message({
              message: "保存失败",
              type: "error",
            });
          } else {
            this.activity = {};
            this.$message({
              message: "添加失败",
              type: "error",
            });
          }
        }
      });
    },
    onCreated(editor) {
        this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
      },
    onChanged() {
      console.log(this.html)
    },
    uploadImg(file, insertFn){
      let imgData = new FormData();
			imgData.append('file', file);
      axios({
        url: this.BASE_URL,
        method: 'post',
        data: imgData,
      }).then((res) => {
       insertFn(res.data.data.url);
      });
    },
    beforeDestroy() {
        const editor = this.editor
        if (editor == null) return
        editor.destroy() // 组件销毁时，及时销毁编辑器
    }
  },
};
</script>

<style src="@wangeditor/editor/dist/css/style.css">

</style>
